package com.mxpio.erp.mes.service.impl;

import com.mxpio.erp.common.mes.entity.SpiLine;
import com.mxpio.erp.common.mes.entity.SpiMain;
import com.mxpio.erp.common.mes.pojo.Spi;
import com.mxpio.erp.common.mes.pojo.SpiLs1;
import com.mxpio.erp.common.mes.pojo.SpiLs2;
import com.mxpio.erp.common.mes.service.SpiMainService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpiMainServiceImpl extends BaseServiceImpl<SpiMain> implements SpiMainService {
    @Override
    @Transactional
    public Result<?> saveFromDl(List<Spi> jsonObjMap) {
        for (Spi spi:jsonObjMap){
            List<SpiLs1> ls1 = spi.getLs1();
            List<SpiLs2> ls2 = spi.getLs2();
            if (ls1==null||ls2==null||ls1.size()==0||ls2.size()==0){
                throw new MBootException("接口请求数据不完整");
            }
            Map<String, List<SpiLs2>> collect = ls2.stream().collect(Collectors.groupingBy(SpiLs2::getArrayID));
            for (SpiLs1 spiLs1:ls1){
                SpiMain spiMain = new SpiMain();
                BeanReflectionUtils.copyProperties(spiMain,spi);
                BeanReflectionUtils.copyProperties(spiMain,spiLs1);
                String arrayID = spiLs1.getArrayID();
                List<SpiLine> spiLines = new ArrayList<>();
                if (collect.containsKey(arrayID)){
                    List<SpiLs2> spiLs2s = collect.get(arrayID);
                    BigDecimal sucCnt = BigDecimal.ZERO;
                    for (SpiLs2 spils2:spiLs2s){
                        if (StringUtils.equalsIgnoreCase("Good",spils2.getResult())
                                ||StringUtils.equalsIgnoreCase("PASS",spils2.getResult())
                                ||StringUtils.equalsIgnoreCase("RPASS",spils2.getResult())){
                            sucCnt = sucCnt.add(BigDecimal.ONE);
                        }else{
                            SpiLine spiLine = new SpiLine();
                            BeanReflectionUtils.copyProperties(spiLine,spils2);
                            spiLine.setXOffset(spils2.getXOffset());
                            spiLine.setYOffset(spils2.getYOffset());
                            spiLines.add(spiLine);
                        }
                    }
                    if (sucCnt.compareTo(BigDecimal.ZERO)!=0){
                        SpiLine spiLine = new SpiLine();
                        spiLine.setArrayID(arrayID);
                        spiLine.setBarcode(spiMain.getBoardBarCode());
                        spiLine.setResult("Good");
                        spiLine.setErrcode("0");
                        spiLine.setCnt(sucCnt);
                        spiLines.add(spiLine);
                    }
                }
                spiMain.setLines(spiLines);
                this.save(spiMain,new SmartCrudPolicyAdapter(){
                    @Override
                    public boolean beforeInsert(CrudContext context) {
                        Object o = context.getEntity();
                        if (o instanceof SpiLine) {
                            SpiMain parent = context.getParent();
                            ((SpiLine) o).setMid(parent.getId());
                        }
                        return true;
                    }
                });
            }
        }
        return Result.OK();
    }
}
